package game

import player.Player
import player.PlayerManager
import poker.PokerManager
import role.PokerCompareTwo
import utile.*
import kotlin.system.exitProcess

class HappyComparePokers:IGame() {
    private var allInStartIndex = INVALIDE_NUM //allin时玩家的索引
    private var lastBet = 0 //上一位玩家下注金额
    private var playerManager = PlayerManager.sharedManager
    private val pokerManager = PokerManager.sharedManager
    private var tableMoney = 0 //桌上下注的总金币
    private var aliveCount = 4 //在玩的玩家数量

        set(value) {
            field = value
            if (aliveCount == 1) {
                playerManager.awardWinner(tableMoney)
                gameOver()
            }
            }
    private var movingIndex = 0 //当前玩家索引
        set(value) {
            field = value
            //判断是不是all in
            if (allInStartIndex!= INVALIDE_NUM){
                //判断一轮结束了么
                if (movingIndex == allInStartIndex){
                    //游戏结束 比大小
                    ""
                    playerManager.showALLpeopleInfomationWithPoker()
                    val player = playerManager.findMaxPlayer()
                    //找最小all-in金币数
                    val minMoney = playerManager.minBetMoney()
                    //下注多的返还
                    val extraMoney = playerManager.returnExtraMoney(minMoney)
                    tableMoney -= extraMoney

                    //奖励
                    playerManager.awardWinner(player,tableMoney)
                    //游戏结束
                    playerManager.showALLpeopleInfomation()
                    gameOver()
                }
            }
        }

    override fun gameStart() {
        gameProgerss()
    }
    fun gameProgerss(){
        var total:Int=0;
        var pepolechoose=0

        playerManager.bottom=SetBottom()
        playerManager.setEveryBottomMoney(playerManager.Players,playerManager.bottom)
        playerManager.givePokerToEveryOne()
        tableMoney=playerManager.bottom*4
        while (true) {
            if(movingIndex>0)
            {
                "请下一名玩家进行操作".showWithEnter()
            }
            if(!playerManager.Players[movingIndex].isAlive)
            {
                continue
            }

            playerManager.showALLpeopleInfomation()
            if(playerManager.isSomeOneAllin||!playerManager.isMoneyEnough(movingIndex,lastBet))
            {
                if(playerManager.isSomeOneAllin)
                {
                    "由于前面玩家有人选择ALL_IN,所以你只能进行如下操作".showWithEnter()
                }
                else{
                    "由于您的金钱不足，所以你只能进行如下操作".showWithEnter()
                }
                OnlyChoicePerson();
            }
            else{
                nomalChoicePerson()
            }
            pepolechoose=playerManager.showNowPersonTable(movingIndex)
            when(pepolechoose){
               1->{//
                   if(allInStartIndex!= INVALIDE_NUM)
                   {
                       Dicard(playerManager.Players[movingIndex])
                   }else{
                         lastBet=Bet(lastBet)
                         tableMoney+=lastBet
                   }

               }
                2->{
                    if(allInStartIndex!= INVALIDE_NUM)
                    {
                        ALL_IN(playerManager.Players[movingIndex])
                    }
                    if(lastBet>playerManager.Players[movingIndex].money)
                    {
                        "不能跟注".showWithEnter()
                    }
                    else{
                        FOLLOW_Bet()
                        tableMoney+=lastBet
                    }

                }
                3->{
                    Pepole_Poker_Compare(playerManager.Players[movingIndex])

                }
                4->{
                    Dicard(playerManager.Players[movingIndex])//弃牌操作
                }
                5->{
                   allInStartIndex=movingIndex
                    ALL_IN(playerManager.Players[movingIndex])
                }

            }

            movingIndex=playerManager.findNextPlayer(movingIndex+1)
            "当前奖池为$tableMoney".showWithEnter()
        }

    }
    fun Dicard(player:Player){
        player.isAlive=false
        aliveCount--
    }

    fun gameOver(){
      "游戏结束".showWithEnter()
        exitProcess(0)
    }
    fun ALL_IN(player:Player){
        player.lastBet=player.money
        player.money=0
        playerManager.isSomeOneAllin=true
    }
    fun Bet(lastBet:Int):Int{

        return getBetMoney(playerManager.Players[movingIndex],lastBet)
    }
    fun FOLLOW_Bet(){
        getFOLLOW_Bet(playerManager.Players[movingIndex],lastBet)
    }
    fun Pepole_Poker_Compare(player: Player){
        "请玩家先进行下注".showWithEnter()
        lastBet=Bet(lastBet)
        tableMoney+=lastBet//当前玩家的投注
        playerManager.showALLpeopleInfomation()
        var other:Player=getCOMPARETORPLAYER(playerManager,movingIndex)
        playerManager.showCompareTwoPoker(player,other)
        if(!PokerCompareTwo(playerManager.Players[movingIndex].poker,other.poker))
        {
            playerManager.Players[movingIndex].isAlive=false
            "${other.name}胜利".showWithEnter()
            aliveCount--
        }
        else{
            other.isAlive = false
            "${player.name}胜利".showWithEnter()
            aliveCount--
        }
        playerManager.Players[other.set]=other
    }
}